﻿using IdentityServer4.AccessTokenValidation;
using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.DependencyInjection;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MyWeb.ServiceBus.Extensions.Authentication
{
    /// <summary>
    /// Ids鉴权
    /// </summary>
    public static class IdsAuthentication
    {
        /// <summary>
        /// 启用Ids鉴权
        /// </summary>
        /// <param name="services"></param>
        /// <exception cref="ArgumentNullException"></exception>
        public static void AddIdsAuthSetup(this IServiceCollection services)
        {
            if (services == null) throw new ArgumentNullException(nameof(services));

            var authenticationProviderKey = "MyWeb.ScheduledTasks";
            #region Ids4校验
            services.AddAuthentication(IdentityServerAuthenticationDefaults.AuthenticationScheme)
                    .AddIdentityServerAuthentication(authenticationProviderKey, option =>
                    {
                        //这里配置是127.0.0.1，那么通过ID4服务器获取token的时候，就必须写127.0.0.1，不能写localhost.
                        option.Authority = "http://127.0.0.1:7001";
                        //必须对应ID4服务器中GetApiResources配置的apiName,此处不能随便写！！
                        option.ApiName = "";
                        option.RequireHttpsMetadata = false;
                    });
            #endregion
        }
    }
}
